<?php
/*
  Gets the html table to manage layby.
 */

function get_layby_manage_table($layby, $controller) {
    $CI = & get_instance();
    $table = '<table class="tablesorter" id="sortable_table">';

    $headers = array(/* '<input type="checkbox" id="select_all" />', */
        lang('common_date'),
        lang('common_customer'),
        lang('common_ph'),
        lang('common_address'),
        lang('common_invoive'),
        lang('common_total'),
        lang('common_paid'),
        lang('common_balance'),
        lang('common_pickup')/* ,
              '&nbsp' */);
    $table.='<thead><tr>';

    $count = 0;
    foreach ($headers as $header) {
        $count++;

        if ($count == 1) {
            $table.="<th class='leftmost'>$header</th>";
        } elseif ($count == count($headers)) {
            $table.="<th class='rightmost'>$header</th>";
        } else {
            $table.="<th>$header</th>";
        }
    }
    $table.='</tr></thead><tbody>';
    $table.=get_layby_manage_table_data_rows($layby, $controller);
    $table.='</tbody></table>';
    return $table;
}

/*
  Gets the html data rows for the people.
 */

function get_layby_manage_table_data_rows($layby, $controller) {
    $CI = & get_instance();
    $table_data_rows = '';

    foreach ($layby as $lay) {
        $table_data_rows.=get_layby_data_row($lay, $controller);
    }

    if (count($layby) == 0) {
        $table_data_rows.="<tr><td colspan='10'><div class='warning_message' style='padding:7px;'>" . lang('common_no_persons_to_display') . "</div></tr></tr>";
    }
    return $table_data_rows;
}

function get_layby_data_row($lay, $controller) {
    $CI = & get_instance();
    $controller_name = strtolower(get_class($CI));
    $width = $controller->get_form_width();
    $start_of_time = date('Y-m-d', 0);
    $today = date('Y-m-d');
    $link = site_url('laybys/invoice/' . $lay->sale_id);
    $table_data_row = '<tr>';
    //$table_data_row.="<td width='5%'><input type='checkbox' id='layby_".$lay->sale_id." value='".$lay->sale_id."'/></td>";
    $table_data_row.='<td width="10%">' . date('d-m-Y', strtotime($lay->sale_time)) . '</td>';
    $table_data_row.='<td width="15%">' . $lay->first_name . ' ' . $lay->first_name . '</td>';
    $table_data_row.='<td width="15%">' . $lay->phone_number . '</td>';
    $table_data_row.='<td width="15%">' . $lay->address_1 . '</td>';
    $table_data_row.='<td width="10%"><a href="'. $link .'">' . $lay->sale_id . '</a></td>';
    $table_data_row.='<td width="7%">' . to_currency($lay->total) . '</td>';
    $table_data_row.='<td width="7%">' . to_currency($lay->paid) . '</td>';
    $table_data_row.='<td width="7%">' . to_currency($lay->balance) . '</td>';
    $table_data_row.='<td width="15%">' . date('d-m-Y', strtotime($lay->pickup)) . '</td>';

    //$table_data_row.='<td width="3%" class="rightmost">'.anchor($controller_name."/view/$person->person_id/width~$width", lang('common_edit'),array('class'=>'thickbox','title'=>lang($controller_name.'_update'))).'</td>';
    $table_data_row.='</tr>';
    return $table_data_row;
}

/*
  Gets the html table to manage people. V4
 */ 

function get_people_manage_table($people,$controller, $filter = false)
{
    
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
        $header_1 = '<input type="checkbox" id="select_all" />';
        $header_last = '&nbsp;';
        if($filter){
            $header_1 = '';
            $header_last = '';
        }
        
        
	$headers = array($header_1, 
	lang('common_last_name'),
	lang('common_first_name'),
	lang('common_email'),
	lang('common_phone_number'),
        lang('common_company'),
	$header_last);
	$table.='<thead><tr>';

	$count = 0;
	foreach($headers as $header)
	{
		$count++;
		
		if ($count == 1)
		{
			$table.="<th class='leftmost'>$header</th>";
		}
		elseif ($count == count($headers))
		{
                        if($header != '')
                            $table.="<th class='rightmost'>$header</th>";
		}
		else
		{       
			$table.="<th>$header</th>";		
		}
	}
	$table.='</tr></thead><tbody>';
	$table.=get_people_manage_table_data_rows($people,$controller, $filter);
	$table.='</tbody></table>';
        if($filter){
            $table .= '
                        <script>
                            $(".customerReport").click(function(){
                                $("#specific_input_data").attr("value", $(this).attr("value"));
                            });
                        </script>
                        ';
        }
	return $table;
}

function get_people_manage_table_data_rows($people,$controller, $filter = false)
{
    
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($people->result() as $person)
	{
		$table_data_rows.=get_person_data_row($person,$controller, $filter);
	}
	
	if($people->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='7'><div class='warning_message' style='padding:7px;'>".lang('common_no_persons_to_display')."</div></tr></tr>";
	}
	
	return $table_data_rows;
}

/*
  Gets the html data rows for the people.
 */

function get_person_data_row($person,$controller, $filter = false)
{
    
	$CI =& get_instance();
	$controller_name=strtolower(get_class($CI));
        
        if($controller_name != 'reports')
            $width = $controller->get_form_width();
        
	$start_of_time =  date('Y-m-d', 0);
	$today = date('Y-m-d');	
	$link = site_url('reports/specific_'.($controller_name == 'customers' ? 'customer' : 'employee').'/'.$start_of_time.'/'.$today.'/'.$person->person_id.'/all/0');
        if($filter)
            $link = '#';
	$table_data_row='<tr>';	
        if($filter){
            $table_data_row.="<td width='5%'><input type='radio' name='customerReport' class='customerReport' id='person_$person->person_id' value='".$person->person_id."'/></td>";
        }else{
            $table_data_row.="<td width='5%'><input type='checkbox' id='person_$person->person_id' value='".$person->person_id."'/></td>";
        }
	$table_data_row.='<td width="15%"><a href="'.$link.'" class="underline">'.$person->last_name.'</a></td>';
	$table_data_row.='<td width="15%"><a href="'.$link.'" class="underline">'.$person->first_name.'</a></td>';
	$table_data_row.='<td width="20%">'.mailto($person->email,$person->email, array('class' => 'underline')).'</td>';
	$table_data_row.='<td width="20%">'.$person->phone_number.'</td>';
        $table_data_row.='<td width="20%">'.$person->company_name.'</td>';
        if(!$filter)
            $table_data_row.='<td width="5%" class="rightmost">'.anchor($controller_name."/view/$person->person_id/width~$width", lang('common_edit'),array('class'=>'thickbox','title'=>lang($controller_name.'_update'))).'</td>';
        
            
	$table_data_row.='</tr>';
        
	return $table_data_row;
}

/*
  Gets the html table to manage suppliers.
 */

function get_supplier_manage_table($suppliers, $controller) {
    $CI = & get_instance();
    $table = '<table class="tablesorter" id="sortable_table">';
    $headers = array('<input type="checkbox" id="select_all" />',
        lang('suppliers_company_name'),
        lang('common_last_name'),
        lang('common_first_name'),
        lang('common_email'),
        lang('common_phone_number'),
        '&nbsp');
    $table.='<thead><tr>';
    $count = 0;
    foreach ($headers as $header) {
        $count++;

        if ($count == 1) {
            $table.="<th class='leftmost'>$header</th>";
        } elseif ($count == count($headers)) {
            $table.="<th class='rightmost'>$header</th>";
        } else {
            $table.="<th>$header</th>";
        }
    }

    $table.='</tr></thead><tbody>';
    $table.=get_supplier_manage_table_data_rows($suppliers, $controller);
    $table.='</tbody></table>';
    return $table;
}

/*
  Gets the html data rows for the supplier.
 */

function get_supplier_manage_table_data_rows($suppliers, $controller) {
    $CI = & get_instance();
    $table_data_rows = '';

    foreach ($suppliers->result() as $supplier) {
        $table_data_rows.=get_supplier_data_row($supplier, $controller);
    }

    if ($suppliers->num_rows() == 0) {
        $table_data_rows.="<tr><td colspan='8'><div class='warning_message' style='padding:7px;'>" . lang('common_no_persons_to_display') . "</div></tr></tr>";
    }

    return $table_data_rows;
}

function get_supplier_data_row($supplier, $controller) {
    $CI = & get_instance();
    $controller_name = strtolower(get_class($CI));
    $width = $controller->get_form_width();

    $table_data_row = '<tr>';
    $table_data_row.="<td width='5%'><input type='checkbox' id='person_$supplier->person_id' value='" . $supplier->person_id . "'/></td>";
    $table_data_row.='<td width="17%">' . $supplier->company_name . '</td>';
    $table_data_row.='<td width="17%">' . $supplier->last_name . '</td>';
    $table_data_row.='<td width="17%">' . $supplier->first_name . '</td>';
    $table_data_row.='<td width="22%">' . mailto($supplier->email, $supplier->email) . '</td>';
    $table_data_row.='<td width="17%">' . $supplier->phone_number . '</td>';
    $table_data_row.='<td width="5%" class="rightmost">' . anchor($controller_name . "/view/$supplier->person_id/width~$width", lang('common_edit'), array('class' => 'thickbox', 'title' => lang($controller_name . '_update'))) . '</td>';
    $table_data_row.='</tr>';
    return $table_data_row;
}

/*
  Gets the html table to manage items.
 */

function get_items_manage_table($items, $controller) {
    $CI = & get_instance();
    $table = '<table class="tablesorter" id="sortable_table">';

    $headers = array('<input type="checkbox" id="select_all" />',
        lang('items_item_number'),
        lang('items_name'),
        lang('items_category'),
        lang('items_cost_price'),
        lang('items_unit_price'),
        //lang('items_tax_percents'),
        lang('items_quantity'),
        lang('items_inventory'),
        '&nbsp;'
    );

    $table.='<thead><tr>';
    $count = 0;
    foreach ($headers as $header) {
        $count++;

        if ($count == 1) {
            $table.="<th class='leftmost'>$header</th>";
        } elseif ($count == count($headers)) {
            $table.="<th class='rightmost'>$header</th>";
        } else {
            $table.="<th>$header</th>";
        }
    }
    $table.='</tr></thead><tbody>';
    $table.=get_items_manage_table_data_rows($items, $controller);
    $table.='</tbody></table>';
    return $table;
}

/*
  Gets the html data rows for the items.
 */

function get_items_manage_table_data_rows($items, $controller) {
    $CI = & get_instance();
    $table_data_rows = '';

    foreach ($items->result() as $item) {
        $table_data_rows.=get_item_data_row($item, $controller);
    }

    if ($items->num_rows() == 0) {
        $table_data_rows.="<tr><td colspan='11'><div class='warning_message' style='padding:7px;'>" . lang('items_no_items_to_display') . "</div></tr></tr>";
    }

    return $table_data_rows;
}

function get_item_data_row($item, $controller) {
    $CI = & get_instance();
    $item_tax_info = $CI->Item_taxes->get_info($item->item_id);
    /* $tax_percents = '';
      foreach($item_tax_info as $tax_info)
      {
      $tax_percents.=$tax_info['percent']. '%, ';
      }
      $tax_percents=substr($tax_percents, 0, -2); */
    $controller_name = strtolower(get_class($CI));
    $width = $controller->get_form_width();

    $table_data_row = '<tr>';
    $table_data_row.="<td width='3%'><input type='checkbox' id='item_$item->item_id' value='" . $item->item_id . "'/></td>";
    $table_data_row.='<td width="15%">' . $item->item_number . '</td>';
    $table_data_row.='<td width="15%">' . $item->name . '</td>';
    $table_data_row.='<td width="11%">' . $item->category . '</td>';
    $table_data_row.='<td width="11%" align="right">' . to_currency($item->cost_price) . '</td>';
    $table_data_row.='<td width="11%" align="right">' . to_currency($item->unit_price) . '</td>';
    //$table_data_row.='<td width="11%">'.$tax_percents.'</td>';	
    $table_data_row.='<td width="11%">' . $item->quantity . '</td>';
    $table_data_row.='<td width="8%">' . anchor($controller_name . "/inventory/$item->item_id/width~$width", lang('common_inv'), array('class' => 'thickbox', 'title' => lang($controller_name . '_count'))) . '&nbsp;&nbsp;&nbsp;&nbsp;' . anchor($controller_name . "/count_details/$item->item_id/width~$width", lang('common_det'), array('class' => 'thickbox', 'title' => lang($controller_name . '_details_count'))) . '</td>'; //inventory details	
    $table_data_row.='<td width="4%" class="rightmost">' . anchor($controller_name . "/view/$item->item_id/width~$width", lang('common_edit'), array('class' => 'thickbox', 'title' => lang($controller_name . '_update'))) . '</td>';

    $table_data_row.='</tr>';
    return $table_data_row;
}

/*
  Gets the html table to manage giftcards.
 */

function get_giftcards_manage_table($giftcards, $controller) {
    $CI = & get_instance();

    $table = '<table class="tablesorter" id="sortable_table">';

    $headers = array('<input type="checkbox" id="select_all" />',
        lang('giftcards_giftcard_number'),
        lang('giftcards_card_value'),
        lang('giftcards_customer_name'),
        '&nbsp',
    );

    $table.='<thead><tr>';
    $count = 0;
    foreach ($headers as $header) {
        $count++;

        if ($count == 1) {
            $table.="<th class='leftmost'>$header</th>";
        } elseif ($count == count($headers)) {
            $table.="<th class='rightmost'>$header</th>";
        } else {
            $table.="<th>$header</th>";
        }
    }
    $table.='</tr></thead><tbody>';
    $table.=get_giftcards_manage_table_data_rows($giftcards, $controller);
    $table.='</tbody></table>';
    return $table;
}

/*
  Gets the html data rows for the giftcard.
 */

function get_giftcards_manage_table_data_rows($giftcards, $controller) {
    $CI = & get_instance();
    $table_data_rows = '';

    foreach ($giftcards->result() as $giftcard) {
        $table_data_rows.=get_giftcard_data_row($giftcard, $controller);
    }

    if ($giftcards->num_rows() == 0) {
        $table_data_rows.="<tr><td colspan='11'><div class='warning_message' style='padding:7px;'>" . lang('giftcards_no_giftcards_to_display') . "</div></tr></tr>";
    }

    return $table_data_rows;
}

function get_giftcard_data_row($giftcard, $controller) {
    $CI = & get_instance();
    $controller_name = strtolower(get_class($CI));
    $width = $controller->get_form_width();
    $link = site_url('reports/detailed_' . $controller_name . '/' . $giftcard->customer_id . '/0');
    $cust_info = $CI->Customer->get_info($giftcard->customer_id);

    $table_data_row = '<tr>';
    $table_data_row.="<td width='3%'><input type='checkbox' id='giftcard_$giftcard->giftcard_id' value='" . $giftcard->giftcard_id . "'/></td>";
    $table_data_row.='<td width="15%">' . $giftcard->giftcard_number . '</td>';
    $table_data_row.='<td width="20%">' . to_currency($giftcard->value) . '</td>';
    $table_data_row.='<td width="15%"><a class="underline" href="' . $link . '">' . $cust_info->first_name . ' ' . $cust_info->last_name . '</a></td>';
    $table_data_row.='<td width="5%" class="rightmost">' . anchor($controller_name . "/view/$giftcard->giftcard_id/width~$width", lang('common_edit'), array('class' => 'thickbox', 'title' => lang($controller_name . '_update'))) . '</td>';

    $table_data_row.='</tr>';
    return $table_data_row;
}

/*
  Gets the html table to manage payment.
 */

function get_payment_manage_table($payment, $controller) {

    $CI = & get_instance();

    $table = '<table class="tablesorter" id="sortable_table">';

    $headers = array('<input type="checkbox" id="select_all" />',
        lang('payment_name'),
        '&nbsp',
    );

    $table.='<thead><tr>';
    $count = 0;
    foreach ($headers as $header) {
        $count++;

        if ($count == 1) {
            $table.="<th class='leftmost'>$header</th>";
        } elseif ($count == count($headers)) {
            $table.="<th class='rightmost'>$header</th>";
        } else {
            $table.="<th>$header</th>";
        }
    }

    $table.='</tr></thead><tbody>';
    $table.=get_payment_manage_table_data_rows($payment, $controller);
    $table.='</tbody></table>';
    return $table;
}

/*
  Gets the html data rows for the payment.
 */

function get_payment_manage_table_data_rows($payments, $controller) {
    $CI = & get_instance();
    $table_data_rows = '';

    foreach ($payments->result() as $payment) {
        $table_data_rows.=get_payment_data_row($payment, $controller);
    }

    if ($payments->num_rows() == 0) {
        $table_data_rows.="<tr><td colspan='3'><div class='warning_message' style='padding:7px;'>" . lang('payment_no_item_kits_to_display') . "</div></tr></tr>";
    }

    return $table_data_rows;
}

function get_payment_data_row($payment, $controller) {

    $CI = & get_instance();


    $controller_name = strtolower(get_class($CI));
    $width = $controller->get_form_width();

    $table_data_row = '<tr>';
    $table_data_row.="<td width='1%'><input type='checkbox' id='payment_$payment->payment_id' value='" . $payment->payment_id . "'/></td>";
    $table_data_row.='<td width=96%">' . $payment->name . '</td>';
    $table_data_row.='<td width="3%" class="rightmost">' . anchor($controller_name . "/view/$payment->payment_id/width~$width", lang('common_edit'), array('class' => 'thickbox', 'title' => lang($controller_name . '_update'))) . '</td>';
    $table_data_row.='</tr>';

    return $table_data_row;
}

/*
  Gets the html table to manage item kits.
 */

function get_item_kits_manage_table($item_kits, $controller) {
    $CI = & get_instance();

    $table = '<table class="tablesorter" id="sortable_table">';

    $headers = array('<input type="checkbox" id="select_all" />',
        lang('items_item_number'),
        lang('item_kits_name'),
        lang('item_kits_description'),
        lang('items_unit_price'),
        lang('items_tax_percents'),
        '&nbsp',
    );

    $table.='<thead><tr>';
    $count = 0;
    foreach ($headers as $header) {
        $count++;

        if ($count == 1) {
            $table.="<th class='leftmost'>$header</th>";
        } elseif ($count == count($headers)) {
            $table.="<th class='rightmost'>$header</th>";
        } else {
            $table.="<th>$header</th>";
        }
    }
    $table.='</tr></thead><tbody>';
    $table.=get_item_kits_manage_table_data_rows($item_kits, $controller);
    $table.='</tbody></table>';
    return $table;
}

/*
  Gets the html data rows for the item kits.
 */

function get_item_kits_manage_table_data_rows($item_kits, $controller) {
    $CI = & get_instance();
    $table_data_rows = '';

    foreach ($item_kits->result() as $item_kit) {
        $table_data_rows.=get_item_kit_data_row($item_kit, $controller);
    }

    if ($item_kits->num_rows() == 0) {
        $table_data_rows.="<tr><td colspan='11'><div class='warning_message' style='padding:7px;'>" . lang('item_kits_no_item_kits_to_display') . "</div></tr></tr>";
    }

    return $table_data_rows;
}

function get_item_kit_data_row($item_kit, $controller) {

    $CI = & get_instance();

    $item_kit_tax_info = $CI->Item_kit_taxes->get_info($item_kit->item_kit_id);
    $tax_percents = '';
    foreach ($item_kit_tax_info as $tax_info) {
        $tax_percents.=$tax_info['percent'] . '%, ';
    }
    $tax_percents = substr($tax_percents, 0, -2);

    $controller_name = strtolower(get_class($CI));
    $width = $controller->get_form_width();

    $table_data_row = '<tr>';
    $table_data_row.="<td width='3%'><input type='checkbox' id='item_kit_$item_kit->item_kit_id' value='" . $item_kit->item_kit_id . "'/></td>";
    $table_data_row.='<td width="15%">' . $item_kit->item_kit_number . '</td>';
    $table_data_row.='<td width="15%">' . $item_kit->name . '</td>';
    $table_data_row.='<td width="20%">' . $item_kit->description . '</td>';
    $table_data_row.='<td width="20%" align="right">' . (!is_null($item_kit->unit_price) ? to_currency($item_kit->unit_price) : '') . '</td>';
    $table_data_row.='<td width="20%">' . $tax_percents . '</td>';
    $table_data_row.='<td width="5%" class="rightmost">' . anchor($controller_name . "/view/$item_kit->item_kit_id/width~$width", lang('common_edit'), array('class' => 'thickbox', 'title' => lang($controller_name . '_update'))) . '</td>';
    $table_data_row.='</tr>';
    return $table_data_row;
}

?>